﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using LoveMusic.Models;

namespace LoveMusic.Helpers
{
    public static class FlickrAPI
    {
        public static string ApiKey
        {
            get;
            set;
        }
        public static List<IMAGE> GetImages(string artistName, int artistID)
        {
            try
            {
                List<IMAGE> imageList = new List<IMAGE>();

                string apikey = ApiKey;
                FlickrNet.Flickr flickr = new FlickrNet.Flickr(apikey);
                FlickrNet.PhotoSearchOptions searchOptions = new FlickrNet.PhotoSearchOptions();
                searchOptions.Tags = artistName+" music";
                FlickrNet.Photos microsoftPhotos = flickr.PhotosSearch(searchOptions);

                //searchOptions. Page = 2;
//                FlickrNet.Photos microsoftPhotos2 = flickr.PhotosSearch(searchOptions);
  //              searchOptions.Page = 3;
    //            FlickrNet.Photos microsoftPhotos3 = flickr.PhotosSearch(searchOptions);


                FlickrNet.PhotoCollection allPhotos = microsoftPhotos.PhotoCollection;
                //allPhotos.AddRange(microsoftPhotos2.PhotoCollection);
                //allPhotos.AddRange(microsoftPhotos3.PhotoCollection);
                int brojac = 1;
                
                foreach (FlickrNet.Photo photo in allPhotos)
                {
                    IMAGE flickrImage = new IMAGE();
                    flickrImage.ARTIST_IDZ = artistID;

                    flickrImage.HEIGHT = photo.OriginalHeight;
                    flickrImage.WIDTH = photo.OriginalWidth;

                    if (brojac == 1)
                        flickrImage.TYPE = "primary";
                    else
                        flickrImage.TYPE = "secondary";

                    flickrImage.URL = photo.MediumUrl;
                    flickrImage.URLTHUMB = photo.SquareThumbnailUrl;

                    imageList.Add(flickrImage);
                }

                return imageList;
            }
            catch (Exception ex)
            {
                TraceLog.TimeWrite(ex, "GetImages - FlickrAPI");
                return null;
            }
        }
    }
}